<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use App\Model\{Admin,User,Config,Message};

/**
 * @menu_cat 消息
 * @icon sunglasses
 * @sort 20
 */
class MessageController extends AdminController{

	public function __invoke(){
		return $this->index(func_get_args());
	}
	
	/**
	 * @menu 消息列表
	 */
	public function index(){
		$list = DB::table('message');
        $this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
       
        return view('Admin.Message.index')->with([
            'list' => $list,
            '_search'=>$search,
        ]);
	}
	/**
	 * @menu 发送系统消息
	 * @author wudi
	 */
	public function send_system(){
		if(request()->isMethod('post')){
			$this->validate(request(), [
				'title'=>'required',
				'content'=>'required',
				'click_id'=>'required',
			]);
			$param = request()->only(['title','content','click_id']);
			$click = config('click');
			$click_info = $click[$param['click_id']];

			$data = [];
	        $data['click_url'] = $click_info['click_url'];
	        $data['button_name'] = $click_info['button_name']??'查看';
	        $data['work_type']='2';
	        $data['content']= $param['content'];
	        $data['type']=2;
	        $data['show_type']=2;
	        DB::table('message')->insert([
	        	'is_system'=>1,
	        	'type'=>2,
	        	'title'=>$param['title'],
	        	'content'=>$param['content'],
	        	'add_time'=>time(),
	        	'click_url'=>$click_info['click_url'],
	        	'button_name'=>$click_info['button_name'],
	        	'show_type'=>2,
	        ]);
			$res = Message::send_system([
				'title'=>$param['title'],
				'data'=>$data,
			]);
			dd($res);
		}

		$click = config('click');
		return view('Admin.Message.send_one')->with([
			'click'=>$click,	
		]);
	}
}